﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.Strippings
{
    public class StrippingCreateHandler : IRequestHandler<StrippingCreateCommand, ResultsDTO>
    {
        #region 仓储
        private readonly IBateRepository<Stripping> StrippingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="StrippingRepository"></param>
        public StrippingCreateHandler(IBateRepository<Stripping> StrippingRepository)
        {
            this.StrippingRepository = StrippingRepository;
        }
        #endregion

        #region 生产退料 新增
        /// <summary>
        /// 生产退料 新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(StrippingCreateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();
                //创建新实例 stripping 通过对象初始化器的方式定义一些属性赋值
                Stripping stripping = new Stripping()
                {
                    StrippingId = request.StrippingId,
                    StrippingNode = request.StrippingNode,
                    StrippingName = request.StrippingName,
                    ProductionWorkOrdersNo = request.ProductionWorkOrdersNo,
                    StashId = request.StashId,
                    StrippingTime = request.StrippingTime,
                    Remark = request.Remark,
                    Documents = 1,
                    StrippingStatus=request.StrippingStatus,
                };

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = "insert into stripping values(@StrippingId,@StrippingNode,@StrippingName,@ProductionWorkOrdersNo,@StashId,@StrippingTime,@Remark,@Documents,@StrippingStatus)";
                //调用StrippingRepository.Add方法，
                //传入构建好的 SQL 插入语句str、stripping对象 数据库连接字符串conn
                result.Code = await StrippingRepository.Add(str, stripping, conn);
                result.Message = "生产退料 新增成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
